var reportSpace = "monthplanning/month-planning!query?page=";
$(function(){
	$(".datepicker" ).datepicker();
	fillDepartments("depts");	
	$("div.pagination ul li a").bind("click",function(){
		
		var page = $(this).attr("page");
		if(deptId==null){
			var link = reportSpace+page+"&rows=10";
			doGet(link);	
		}else{
			var url = reportSpace+page+"&rows=10&query.fields[0].name=department.id&query.fields[0].value="+deptId;
			doPost(url);
		}
		
		
	});
});

/** 填充部门控件 */
function fillDepartments(deptId) {
	var depts = $("#"+deptId);
	$.getJSON('admin/department/department!bytype',
			{
		      "bean.id":"root",
		      "bean.departmentType":"Manufacture"
		
			},
			
		function(data, textStatus, jqXHR) {

		$.each(data, function(i, item) {
			var ipt = $("<button type='button' class='btn btn-info' dept='"+item['id']+"'>"+item['name']+"</button>");
			ipt.bind("click",function(){
				departmentClicked($(this).attr("dept"),$(this).html());
			});
			depts.append(ipt);			
		});	
		
	});
}
function departmentClicked(dept,text){
	deptId = dept;	
	var url = reportSpace+"0&rows=10&query.fields[0].name=department.id&query.fields[0].value="+deptId;
	doPost(url);	
}




function newOne(){
	var url= "monthplanning/prepare";
	window.open(url,'pwin');
}
function removeOne(id) {
	var that = getEvent().srcElement;
	$.ajax({
		url : "monthplanning/month-planning!remove",
		data : "bean.id=" + id,
		success : function() {					
			$(that).parent().parent().remove();			
		}
	});
}
function showOne(id){
	var url = "monthplanning/month-planning!get?bean.id="+id;
	window.open(url,'myplan');
}

function planExport(){
   var date = $(".datepicker" ).val();
   if(!date){
	   alert("请先选择日期");
	   return;
   }
   var form = $("#dlform");
   $("#adate").val(date);  
   form.get(0).submit();
}